﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.Models;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminColorController : Controller
    {
        //
        // GET: /Admin/Color/

        public ActionResult Index(int page = 1)
        {
            var itemsPerPage = 10;
            var totalPage = 0;
            var model = ColorDb.GetAllColor(page, itemsPerPage, ref totalPage) ?? new List<ColorListDto>();
            ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage};
            return View(model);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(ColorDto model)
        {
            model.CreateDate = DateTime.Now;
            model.ModifyDate = model.CreateDate;
            model.Id = Utility.Common.GenerateId();
            var result = ColorDb.Add(model);
            if(result > 0)
                return RedirectToAction("Index", "AdminColor");
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var model = ColorDb.GetById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ColorDto model)
        {
            model.ModifyDate = DateTime.Now;
            ColorDb.Update(model);
            return View(model);
        }

        public JsonResult DeleteColor(string id)
        {
            var result = ColorDb.DeleteColor(id);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }
    }
}
